import { ValidationError, validateSync } from 'class-validator'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function validateClass(object: any, clazz?: any): ValidationError[] {
    if (clazz != null) {
        if (typeof object === 'string') {
            object = JSON.parse(object)
        }
        const objTmp = new clazz
        Object.assign(objTmp, object)
        return validateSync(objTmp)
    } else {
        // 如果object是原始Object，但是又没有clazz，则异常
        if (!(object instanceof Object) || object.constructor === {}.constructor) {
            throw new Error('object的类型不是自定义class类型，无法校验')
        }
        return validateSync(object)
    }

}